// pages/message/message.js
var xrRequest = require('../../vendor/hope-sdk/core/request.js');
var xrSdk = require('../../vendor/hope-sdk/index.js');
var config = require('../../config.js');
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    messageList: [],
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    noContent: false,

    img: {
      url: "/static/images/logo.png",
      width: 80,
      height: 80,
      circle: true
    }
  },
  initData: function () {
    this.setData({
      messageList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app, {
      url: config.service.messageList,
      login: true,
      data: params,
      success: function (res) {
        that.setData({
          messageList: that.data.messageList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.messageList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.messageList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../auth/auth"
        })
      }
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10
    };
    return params;
  },
  read: function (id) {
    var that = this;
    xrRequest.request(app, {
      url: config.service.messageRead,
      login: false,
      data: {
        id: id
      },
      success: function (res) {
        if (res.data.code == 1) {

        }
      }
    })
  },
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData();
    this.initList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
      * 生命周期函数--监听页面显示
      */
  onShow: function () {
    this.onPullDownRefresh();

    // wx.hideTabBar({
    //   success: function () {
    //     app.editTabbar();
    //   }
    // });
    app.checkMessageCount();
  },
  tabbarSwitch: function (e) {
    if (e.detail != undefined) {
      wx.switchTab({
        url: e.detail.pagePath
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  go: function (e) {
    var url = e.currentTarget.dataset.url;
    var id = e.currentTarget.dataset.id;
    this.read(id);
    if(url){
      if (url.indexOf('../my/my') != -1) {
        wx.switchTab({
          url: url
        })
      } else {
        wx.navigateTo({
          url: url
        });
      }
    }
  },
  callPhone:function(e){
    wx.makePhoneCall({
      phoneNumber: '4000339066' //仅为示例，并非真实的电话号码
    })
  }
})